package net.xdclass.controller;


import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.component.SmsComponent;
import net.xdclass.util.JsonData;
import net.xdclass.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tiezhu
 * @since 2024-07-17
 */
@Slf4j
@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private SmsComponent smsComponent;

    @GetMapping("/test")
    public JsonData testNacosConfig(){
        return JsonData.buildSuccess("nacos config test success");
    }

    //发送短信验证码
    @GetMapping(value = "/send/{phone}")
    public JsonData msgSend(@PathVariable String phone) {
        //1、从redis中获取验证码，如果获取到就直接返回
        String code = redisTemplate.opsForValue().get(phone);
        if(!StringUtils.isEmpty(code)){
            return JsonData.buildSuccess("短信已发送，未过期");
        }else {
            //2、如果获取不到，就进行阿里云发送
            code = RandomUtil.getSixBitRandom();//生成验证码的随机值
            Map<String,Object> param = new HashMap<>();
            param.put("code", code);
            try {
                smsComponent.send(phone, param);
                redisTemplate.opsForValue().set(phone, code,5, TimeUnit.MINUTES);
                return JsonData.buildSuccess("短信发送成功");
            }catch (Exception e){
                return JsonData.buildError("短信发送失败");
            }
        }
    }

    @GetMapping(value = "/test/{phone}")
    public JsonData testSend(@PathVariable String phone) {
        //1、从redis中获取验证码，如果获取到就直接返回
        Integer count=null;
        String countS = redisTemplate.opsForValue().get("count");
        if (StringUtils.isEmpty(countS)){
            redisTemplate.opsForValue().set("count","0");
            count=0;
        }else {
            count= Integer.valueOf(countS);
        }
        String code = redisTemplate.opsForValue().get(phone);
        if(!StringUtils.isEmpty(code)){
            return JsonData.buildSuccess("短信已发送，未过期");
        }else {
            //2、如果获取不到，就进行阿里云发送
            code = RandomUtil.getSixBitRandom();//生成验证码的随机值
            Map<String,Object> param = new HashMap<>();
            param.put("code", code);
            try {
//                smsComponent.send(phone, param);
                synchronized (count){
                    count++;
                    redisTemplate.opsForValue().set(phone+"_"+count, code,30, TimeUnit.SECONDS);
                    redisTemplate.opsForValue().set("count",String.valueOf(count));
                    log.info(Thread.currentThread().getName()+"-"+System.currentTimeMillis()+":"+phone+"_"+count+":"+code);
                }

                return JsonData.buildSuccess("短信发送成功");
            }catch (Exception e){
                return JsonData.buildError("短信发送失败");
            }
        }
    }

}

